using NaughtyAttributes;
using UnityEngine;

namespace Gj
{
    public class DragArrowHelper : NodeHelper
    {
        public GameObject arrow;
        public bool invert;
        public Vector3 p;
        private bool allow;
        protected override BindDataType bindDataType => BindDataType.Bool;
        
        [Button]
        protected override void Start()
        {
            p = transform.position;
        }

        private void Update()
        {
            if (!allow) return;
            Refresh();
        }

        protected override void BindData(bool b)
        {
            allow = invert ? !b : b;
            arrow.SetActive(allow);
            if (!allow) return;
            Refresh();
        }

        void Refresh()
        {
            // transform.position = SystemInput.cp;
            UpdatePosition(transform.position, SystemInput.cp);
            Emit(UIEvent.Drag);
        }

        public void UpdatePosition(Vector3 s, Vector3 e)
        {
            Vector3 c = (s + e) / 2;
            Vector3 ec = e - c;
            RectTransform rt = arrow.GetComponent<RectTransform>();
            rt.sizeDelta = new Vector2(35, Vector3.Distance(s, e) / SystemInput.scale);
            arrow.transform.position = c;
            arrow.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Vector3.SignedAngle(Vector3.up, ec, Vector3.forward)));
        }
    }
}